package edu.njucm.herbiv.service.impl;

import edu.njucm.herbiv.dao.TCMRepository;
import edu.njucm.herbiv.pojo.entity.TCM;
import edu.njucm.herbiv.service.TCMService;
import edu.njucm.herbiv.utils.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @Author ZhouHang
 * @Date 24/4/22 022
 */
@Service
public class TCMServiceImpl implements TCMService {
    @Resource
    private TCMRepository tcmRepository;

    @Override
    public Page<TCM> list(String type, String content, int page, int size) {
        String searchContent = "%" + content + "%";
        Pageable pageable = PageUtils.getPage(page, size, "id");
        return switch (type) {
            case "id" -> tcmRepository.findAllByIdLike(searchContent, pageable);
            case "cnName" -> tcmRepository.findAllByCnNameLike(searchContent, pageable);
            case "enName" -> tcmRepository.findAllByEnNameLike(searchContent, pageable);
            default -> tcmRepository.findAll(pageable);  // 查询全部
        };
    }
}
